package com.example.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MImageView extends ImageView {

	private PointF startPointF = new PointF();// 记录手指尖移动开始的距离

	private Matrix currentMatrix = new Matrix();// 记录当前位置
	private Matrix newMatrix = new Matrix();// 操作后的位置

	private int mode;// 手势

	private int DRAG = 1;// 拖动
	private int ZOOM = 2;// 放大

	private float startDistance;// 手指放大开始距离
	private float endDistance;// 手指放大结束距离
	
	private int mWidth;

	public MImageView(Context context) {
		super(context);
		currentMatrix.set(getImageMatrix());
	}

	public MImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		currentMatrix.set(getImageMatrix());
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = this.getMeasuredWidth();
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {// 多点触控操作
		case MotionEvent.ACTION_DOWN:// 一个手指点击
			// 单手移动
			mode = DRAG;
			currentMatrix.set(getImageMatrix());
			startPointF.set(event.getX(), event.getY());
			break;

		case MotionEvent.ACTION_MOVE:// 移动
			if (mode == DRAG) {// 如果是拖动
				float mX = event.getX() - startPointF.x;// x轴移动的距离
				float mY = event.getY() - startPointF.y;// y轴移动的距离
				newMatrix.set(currentMatrix);
				newMatrix.preTranslate(mX, mY);

				// postTranslate 位移叠加
				// translate不叠加
				// pretranslate

			} else if (mode == ZOOM) {// 放大
				endDistance = getDistance(event);
				if (endDistance > 10f) {
					float scale = endDistance / startDistance;
					newMatrix.set(currentMatrix);
					newMatrix.postScale(scale, scale, 0, 0);// 設置新的位置
				}
			}
			break;

		case MotionEvent.ACTION_POINTER_DOWN:// 第2只手指点击
			mode = ZOOM;
			startDistance = getDistance(event);

			if (startDistance > 10f) {// 距离太短不考虑
				currentMatrix.set(getImageMatrix());
			}
			break;

		case MotionEvent.ACTION_UP:// 所有手指离开
			mode = 0;
			break;

		case MotionEvent.ACTION_POINTER_UP:// 单个手指松开
			mode = 0;
			break;

		default:
			mode = 0;
			break;
		}
		this.setImageMatrix(newMatrix);
		return true;
	}

	/**
	 * 获取两个手指间的距离
	 * 
	 * @param event
	 * @return
	 */
	private float getDistance(MotionEvent event) {
		float dx = event.getX(1) - event.getX(0);
		float dy = event.getY(1) - event.getY(0);
		return (float) Math.sqrt(dx * dx + dy * dy);
	}

	/**
	 * 获取手指中间的位置
	 * 
	 * @param event
	 * @return
	 */
	public PointF getMidPointF(MotionEvent event) {
		float midx = event.getX(1) + event.getX(0);
		float midy = event.getY(1) + event.getY(0);
		return new PointF(midx, midy);
	}

	OnZoomListener listener;

	public interface OnZoomListener {
		public void onZoomChanged(float scale);

	}

	public void setZoomListener(OnZoomListener listener) {
		this.listener = listener;
	}

	/**
	 * 对外的方法设置放大缩小
	 * 
	 * @param zoom
	 */
	public void setZoom(int zoom) {
		Message msMessage = new Message();
		msMessage.arg1 = zoom;
		mHandler.sendMessage(msMessage);
	}
	
	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			float zoom = (float) 1 + msg.arg1/1000.0f;
			
			System.out.println(mWidth);
			
			currentMatrix.setScale(zoom, zoom, mWidth / 2, 0);
			setImageMatrix(currentMatrix);
		};
	};
	
}
